const toString = Object.prototype.toString

export function is(val: unknown, type: string) {
  return toString.call(val) === `[object ${type}]`
}

export function isDefined<T = unknown>(val?: T): val is T {
  return typeof val !== 'undefined'
}

export function isUnDefined<T = unknown>(val?: T): val is T {
  return !isDefined(val)
}

export function isObject(val: any): val is Record<any, any> {
  return val !== null && is(val, 'Object')
}

export function isObjectAttribute(obj: Object, attr: string): any {
  return Object.keys(obj).indexOf(attr) > -1
}

export function isEmpty<T = unknown>(val: T): val is T {
  if (!val && !isBoolean(val) && !isNumber(val)) return true
  if (isArray(val) || isString(val)) {
    return val.length === 0
  }

  if (val instanceof Map || val instanceof Set) {
    return val.size === 0
  }

  if (isObject(val)) {
    return Object.keys(val).length === 0
  }

  return false
}

export function isDate(val: unknown): val is Date {
  return is(val, 'Date')
}

export function isNull(val: unknown): val is null {
  return val === null
}

export function isNullAndUnDef(val: unknown): val is null | undefined {
  return isUnDefined(val) && isNull(val)
}

export function isNullOrUnDef(val: unknown): val is null | undefined {
  return isUnDefined(val) || isNull(val)
}

export function isNumber(val: unknown): val is number {
  return is(val, 'Number')
}

export function isPromise<T = any>(val: unknown): val is Promise<T> {
  return (
    is(val, 'Promise') &&
    isObject(val) &&
    isFunction(val.then) &&
    isFunction(val.catch)
  )
}

export function isString(val: unknown): val is string {
  return is(val, 'String')
}

export function isFunction(val: unknown): val is Function {
  return typeof val === 'function'
}

export function isBoolean(val: unknown): val is boolean {
  return is(val, 'Boolean')
}

export function isRegExp(val: unknown): val is RegExp {
  return is(val, 'RegExp')
}

export function isArray(val: any): val is Array<any> {
  return val && Array.isArray(val)
}

export function isWindow(val: any): val is Window {
  return typeof window !== 'undefined' && is(val, 'Window')
}

export function isElement(val: unknown): val is Element {
  return isObject(val) && !!val.tagName
}

export const isServer = typeof window === 'undefined'

export const isClient = !isServer

/**
 * @description url Reg
 *
 **/
export function isUrl<T>(value: T): boolean {
  const reg =
    /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/
  // @ts-expect-error
  return reg.test(value)
}

/**
 *
 * @param {*} a
 * @param {*} b
 * @returns
 */
export function isEqual<T = any>(a: T, b: T) {
  const classNameA = toString.call(a)
  const classNameB = toString.call(b)
  if (classNameA !== classNameB) {
    return false
  } else {
    if (classNameA.includes('Object')) {
      for (const key in a) {
        if (!isEqual(a[key], b[key])) return false
      }
      return true
    } else if (classNameA.includes('Array')) {
      if (a.length !== b.length) {
        return false
      } else {
        if (a.length > 0) {
          for (let i = 0; i < a.length; i++) {
            if (!isEqual(a[i], b[i])) return false
          }
          return true
        }
        return true
      }
    } else if (classNameA.includes('Function')) {
      return a.toString() === b.toString()
    } else {
      return Object.is(a, b)
    }
  }
}